import { setToken, removeToken } from "@/util/auth";
import { Message } from "element-ui";
import { setStore, getStore } from "@/util/store";
import { deepClone } from "@/util/util";
import {
  loginByUsername,
  getUserInfo,
  getMenu,
  getTopMenu,
  logout,
  refreshToken,
} from "@/api/user";
import { formatPath } from "@/router/avue-router";
const user = {
  state: {
    userInfo: {},
    permission: {},
    roles: [],
    menuId: {},
    menu: getStore({ name: "menu" }) || [],
    menuAll: getStore({ name: "menuAll" }) || [],
    token: getStore({ name: "token" }) || "",
    refreshToken: getStore({ name: "refreshToken" }) || "",
  },
  actions: {
    //根据用户名登录
    LoginByUsername({ commit }, user) {
      // const user = encryption({
      //   data: userInfo,
      //   type: 'Aes',
      //   key: 'avue',
      //   param: ['useranme', 'password']
      // });
      return new Promise((resolve, reject) => {
        loginByUsername(user.account, user.password, user.code, user.redomStr)
          .then((res) => {
            const data = res.data;
            if (data.status !== 200)
              return Message({ message: data.msg, type: "error" });
            commit("SET_TOKEN", data.token);
            commit("SET_REFRESH_TOKEN", data.token);
            commit("DEL_ALL_TAG");
            commit("CLEAR_LOCK");
            resolve();
          })
          .catch((err) => {
            reject(err);
            console.log(err);
          });
      });
    },
    GetUserInfo({ commit }) {
      return new Promise((resolve) => {
        getUserInfo()
          .then((res) => {
            const data = res.data;
            commit("SET_USERIFNO", data.user);
            commit("SET_ROLES", data.role);
            resolve(data);
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
    //刷新token
    RefreshToken({ state, commit }) {
      return new Promise((resolve, reject) => {
        refreshToken(state.refreshToken)
          .then((res) => {
            const data = res.data.data;
            commit("SET_TOKEN", data);
            commit("SET_REFRESH_TOKEN", data);
            resolve(data);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 登出
    LogOut({ commit }) {
      return new Promise((resolve, reject) => {
        logout()
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_REFRESH_TOKEN", "");
            commit("SET_MENUALL_NULL", []);
            commit("SET_MENU", []);
            commit("SET_ROLES", []);
            commit("DEL_ALL_TAG", []);
            commit("CLEAR_LOCK");
            commit("SET_MENUID", "");
            commit("SET_USERIFNO", {});
            removeToken();
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    //注销session
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        commit("SET_REFRESH_TOKEN", "");
        commit("SET_MENUALL_NULL", []);
        commit("SET_MENU", []);
        commit("SET_ROLES", []);
        commit("DEL_ALL_TAG", []);
        commit("CLEAR_LOCK");
        removeToken();
        resolve();
      });
    },
    GetTopMenu() {
      return new Promise((resolve) => {
        getTopMenu().then((res) => {
          const data = res.data.data || [];
          resolve(data);
        });
      });
    },
    //获取系统菜单
    GetMenu({ commit }) {
      return new Promise((resolve) => {
        getMenu({ role_id: user.state.userInfo.role_id }).then((res) => {
          const data = res.data.data;
          let menu = deepClone(data);
          menu.forEach((ele) => formatPath(ele, true));
          commit("SET_MENUALL", menu);
          commit("SET_MENU", menu);
          resolve(menu);
        });
      });
    },
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      setToken(token);
      state.token = token;
      setStore({ name: "token", content: state.token });
    },
    SET_REFRESH_TOKEN: (state, token) => {
      state.refreshToken = token;
      setStore({ name: "refreshToken", content: state.token });
    },
    SET_MENUID(state, menuId) {
      state.menuId = menuId;
    },
    SET_USERIFNO: (state, userInfo) => {
      state.userInfo = userInfo;
    },
    SET_MENUALL: (state, menuAll) => {
      let menu = state.menuAll;
      menuAll.forEach((ele) => {
        let index = menu.findIndex((item) => item.path == ele.path);
        if (index == -1) {
          menu.push(ele);
        } else {
          menu[index] = ele;
        }
      });
      state.menuAll = menu;
      setStore({ name: "menuAll", content: state.menuAll });
    },
    SET_MENUALL_NULL: (state) => {
      state.menuAll = [];
      setStore({ name: "menuAll", content: state.menuAll });
    },
    SET_MENU: (state, menu) => {
      state.menu = menu;
      setStore({ name: "menu", content: state.menu });
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    // SET_PERMISSION: (state, permission) => {
    //   state.permission = {};
    //   permission.forEach(ele => {
    //     state.permission[ele] = true;
    //   });
    // }
  },
};
export default user;
